package com.sw.test01.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.sw.test01.R;
import com.sw.test01.domain.bean.Msg;
import com.sw.test01.ui.adapter.MsgAdapter;

import java.util.ArrayList;
import java.util.List;

public class TalkActivity extends BaseActivity {

    private List<Msg> msgList = new ArrayList<>();
    private RecyclerView rvTalk;
    private EditText etContent;
    private Button btnSend;
    private MsgAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_talk);
        rvTalk = (RecyclerView) findViewById(R.id.rv_talk);
        etContent = (EditText) findViewById(R.id.et_content);
        btnSend = (Button) findViewById(R.id.btn_send);
        initData();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rvTalk.setLayoutManager(layoutManager);
        adapter = new MsgAdapter(msgList);
        rvTalk.setAdapter(adapter);

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = etContent.getText().toString().trim();
                if (!content.isEmpty()){
                    Msg msg = new Msg(content,Msg.TYPE_SENT);
                    msgList.add(msg);
                    adapter.notifyItemInserted(msgList.size()-1);//有新消息通知更新
                    rvTalk.scrollToPosition(msgList.size()-1);//rec定位到最后一行
                    etContent.setText("");
                }
            }
        });
    }

    private void initData() {
        Msg msg1 = new Msg("Hello guy.", Msg.TYPE_RECEIVED);
        msgList.add(msg1);
        Msg msg2 = new Msg("Hello. Who is that?", Msg.TYPE_SENT);
        msgList.add(msg2);
        Msg msg3 = new Msg("This is Tom. Nice talking to you. ", Msg.TYPE_RECEIVED);
        msgList.add(msg3);
    }
}
;